import { Component } from '@angular/core';
import { LogonService } from "../../../../shared/services/logon.services";
import { NavController } from "ionic-angular";
import { App } from "ionic-angular";
import { HttpService } from 'http-service';
import { QixinLoginPage } from "../qixin-login/qixin-login.page";
import { CardSettingsPage } from "../../../card/page/card-settings/card-settings.page";
import { SecureStorageService } from '../../../../shared/services/secure-storage.services';
import {SqliteService} from "../../../../shared/services/sqliteDB/sqlite.service";
import { API } from "../../../../shared/models/api-path-extension";
import { UtilsService } from "../../../../shared/services/utils.services";
import {NetStatus, SyncPushService} from "../../../../shared/services/offline/sync-push.service";
import { AppVersion } from '@ionic-native/app-version';
import { Platform } from "ionic-angular";
import { Device } from "@ionic-native/device";
import { Network } from "@ionic-native/network";

declare let window: any;

@Component({
  selector: 'my-profile',
  templateUrl: 'my-profile.page.html'
})
export class MyProfilePage {

  userInfo: any;
  appVersionNumber: string;

  constructor(
    public logonService: LogonService,
    public navCtrl: NavController,
    public app: App,
    public httpService: HttpService,
    private dbservice: SqliteService,
    private utilsService: UtilsService,
    private syncPushService: SyncPushService,
    private storage: SecureStorageService,
    private appVersion: AppVersion,
    private device: Device,
    private platform: Platform,
    private network: Network

  ) {
    // TODO
    this.storage.get('userInfo').subscribe((res) => {
      if (res) {
        this.userInfo = res["data"];
      } else {
        if (!this.device.cordova || new RegExp(this.network.type).test(NetStatus.work)) {
          this.utilsService.showLoading();
          this.httpService.get(new API().path({ path: "userInfo" }).baseURL, null)
            .subscribe((response) => {
              console.log('个人信息' + response);
              this.utilsService.hiddenLoading();
              this.storage.set('userInfo', response);
              this.userInfo = response["data"];
            });
        }
      }
    });
  }

  ionViewDidEnter() {
// TODO
    if ( window.cordova ) {
     this.appVersion.getVersionNumber().then((res) => { this.appVersionNumber = res; });
    }
    else {
      this.appVersionNumber = "1.0.0";
    }
  }

  loginOut() {
      this.storage.remove("userInfo");
      this.storage.remove("home_card_user_list");
      this.storage.removeToken("access_token")
        .subscribe(() => {
          this.app.getRootNav().setRoot(QixinLoginPage);
        }, () => {
          this.utilsService.tip("退出失败");
        });

  }

  gotoCardSettings() {
    this.navCtrl.push(CardSettingsPage);
  }
}
